package ua.races.kpiEpam;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Scanner;


/**
 * Input information from console
 */
public class ConsoleInput {
    public static List<Race> input(ResourceBundle bundle) {

        String ans, bid_ans, race_ans, ansn, anso;
        Scanner s = null;
        List<Race> races = new ArrayList<Race>();
        Race race = null;
        Bid bid = null;
        Client client = null;
        DecoratedBid dBid = null;

        do {
            System.out.println(bundle.getString("race.input.moreRaces"));
            s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
            race_ans = s.next();
            race = new Race();
        } while (!(race_ans.toLowerCase().equals("y") || race_ans.toLowerCase().equals("n")));

        while (race_ans.toLowerCase().equals("y")) {
            do {
                System.out.println(bundle.getString("race.input.stadium"));
                s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                ans = s.next();
            } while (ans.toLowerCase().matches("^\\s*$"));
            race.setStadium(ans);

            do {
                System.out.println(bundle.getString("race.input.moreBids"));
                s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                bid_ans = s.next();
                bid = new Bid();
                dBid = new DecoratedBid(bid);
            } while (!(bid_ans.toLowerCase().equals("y") || bid_ans.toLowerCase().equals("n")));

            while (bid_ans.equals("y")) {

                System.out.println(bundle.getString("race.input.bid"));
                do {
                    System.out.println(bundle.getString("race.input.bidType"));
                    s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                    ans = s.next();
                } while (!ans.toUpperCase().matches("^[123]$"));
                bid.setBidType(BidType.valueOf("BID_TYPE"+ans));

                //client = new Client();
                do {
                    System.out.println(bundle.getString("race.input.clientName"));
                    s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                    ansn = s.next();
                } while (ansn.toLowerCase().matches("^\\s*$"));

                do {
                    System.out.println(bundle.getString("race.input.clientOrganization"));
                    s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                    anso = s.next();
                } while (anso.toLowerCase().matches("^\\s*$"));
               // client.setOrganization(ans);
                dBid.setClient(ansn,anso);


                do {
                    System.out.println(bundle.getString("race.input.bidValue"));
                    s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                    ans = s.next();
                } while (!ans.toLowerCase().matches("^\\d*\\.?\\d*$"));
                bid.setValue(Double.valueOf(ans));


                do {
                    System.out.println(bundle.getString("race.input.moreBids"));
                    s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                    bid_ans = s.next();
                    race.addBid(bid);
                    bid = new Bid();
                } while (!(bid_ans.toLowerCase().equals("y") || bid_ans.toLowerCase().equals("n")));
            }     //process of inputing bids

            do {
                System.out.println(bundle.getString("race.input.moreRaces"));
                s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
                race_ans = s.next();
                races.add(race);
                race = new Race();
            } while (!(race_ans.toLowerCase().equals("y") || race_ans.toLowerCase().equals("n")));
        }

        return races;
    }
}
